/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.model.ShopBankCard;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopBankCardService;
import com.yami.shop.service.ShopWithdrawCashService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * 商家提现申请信息
 *
 * @author YXF
 *  * @date 2020-04-07 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopBankCard" )
public class ShopBankCardController {

    private final ShopBankCardService shopBankCardService;
    private final ShopWithdrawCashService shopWithdrawCashService;



    @GetMapping("/getShopBankCardList")
    public ResponseEntity< List<ShopBankCard>> getShopInfo() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ShopBankCard> list = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus,1));
        return ResponseEntity.ok(list);
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCard.setShopId(shopId);
        shopBankCardService.insert(shopBankCard);
        return ResponseEntity.ok().build();
    }

    /**
     * 设置默认银行卡
     */
    @PutMapping("/setDefault")
    public ResponseEntity<Void> setDefault(@RequestBody @Valid ShopBankCard shopBankCard) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopBankCardService.setDefault(shopBankCard.getShopBankCardId(),shopId);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{shopBankCardId}")
    public ResponseEntity<Void> removeById(@PathVariable("shopBankCardId") Long shopBankCardId) {
        ShopBankCard shopBankCard = shopBankCardService.getById(shopBankCardId);
        if (Objects.equals(shopBankCard.getIsDefault(),1)){
            // 不能删除默认银行卡
            throw new YamiShopBindException("yami.shop.cannot.delete.card");
        }
        int count = shopWithdrawCashService.count(new LambdaQueryWrapper<ShopWithdrawCash>()
                .eq(ShopWithdrawCash::getShopBankCardId, shopBankCardId).eq(ShopWithdrawCash::getStatus, 0));
        if(count > 0){
            // 正在用于申请提现的银行卡不能删除
            throw new YamiShopBindException("yami.shop.cannot.delete.check");
        }
        // 进行逻辑删除
        shopBankCardService.update(new LambdaUpdateWrapper<ShopBankCard>().set(ShopBankCard::getStatus,-1).eq(ShopBankCard::getShopBankCardId,shopBankCardId));
        return ResponseEntity.ok().build();
    }
}
